﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: WebResource("ExtJSExtender.Controls.ExtJSControlBase.js", "text/javascript")]

namespace ExtJSExtender.Controls
{
    public abstract class ExtJSControlBase : WebControl, IScriptControl
    {
        #region 属性

        private ScriptManager sm;

        #endregion

        #region 构造函数

        public ExtJSControlBase(HtmlTextWriterTag objTag)
            : base(objTag)
        {
            
        }

        #endregion

        #region override WebControl Method

        /// <summary>
        /// Overrides OnPreRender.  Registers scripts common to the ExtExtenders library
        /// </summary>
        /// <param name="e">The event arguments</param>
        protected override void OnPreRender(EventArgs e)
        {
            if (!DesignMode)
            {
                // Test for ScriptManager and register if it exists
                sm = ScriptManager.GetCurrent(Page);

                if (sm == null)
                    throw new HttpException("A ScriptManager control must exist on the current page.");

                sm.RegisterScriptControl(this);
            }
            
            base.OnPreRender(e);
        }

        /// <summary>
        /// Overrides Render().  Registers script descriptors.
        /// </summary>
        /// <param name="writer">The HtmlTextWriter</param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (!DesignMode)
                sm.RegisterScriptDescriptors(this);
            base.Render(writer);
        }

        #endregion

        #region IScriptControl 成员

        /// <summary>
        /// Abstract method used to get the script descriptors
        /// </summary>
        /// <returns>ScriptDescriptor collection</returns>
        public virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            return new ScriptDescriptor[] { };
        }

        /// <summary>
        /// Abstract method to get script references
        /// </summary>
        /// <returns>Script references</returns>
        public virtual IEnumerable<ScriptReference> GetScriptReferences()
        {
            var reference = new ScriptReference();
            reference.Assembly = "ExtJSExtender.Controls";
            reference.Name = "ExtJSExtender.Controls.ExtJSControlBase.js";

            return new[] { reference };
        }


        #endregion
    }

    public class ExtJSEventArgs : EventArgs
    {
        public string EventName { get; set; }
        public string Arguments { get; set; }
    }
}
